package cn.ynrcb.personnelfile.sysadmin.controller;


import cn.ynrcb.personnelfile.common.utils.PageUtils;
import cn.ynrcb.personnelfile.common.utils.R;
import cn.ynrcb.personnelfile.sysadmin.entity.SysLogEntity;
import cn.ynrcb.personnelfile.sysadmin.service.SysLogService;
import cn.ynrcb.personnelfile.sysadmin.service.SysUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/sys/log")
public class SysLogController {
	@Autowired
	private SysLogService sysLogService;
	@Autowired
	private SysUserService sysUserService;

	/**
	 * 列表
	 */
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:log:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = sysLogService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 列表
	 */
	@ResponseBody
	@GetMapping("/loginLog")
	@RequiresPermissions("sys:log:list")
	public R loginLog(@RequestParam Map<String, Object> params){
		PageUtils page = sysLogService.queryPage(params);
		if(CollectionUtils.isNotEmpty(page.getList())) {
			List<SysLogEntity> logs = (List<SysLogEntity>) page.getList();
			for(SysLogEntity log : logs) {
				if(StringUtils.isNotEmpty(log.getUsername())) {
					Map<String, String> userInfo = sysUserService.getUserInfo(log.getUsername());
					if(null != userInfo) {
						String username = userInfo.get("userCnName")+" ("+userInfo.get("name")+")";
						log.setUsername(username);
					}
				}
			}
		}
		return R.ok().put("page", page);
	}

}
